#encoding=utf8
import os, re, sys
import hashlib, zipfile

apkname="toybr_latest.apk"
chance=1.0000


def mkmd5fromstrc(strc):
    md5 = hashlib.new("md5")
    md5.update(strc)
    return md5.hexdigest()

def mkmd5fromfile(filename):
    fobj = open(filename, "rb")
    md5 = hashlib.new("md5")
    while True:
        fb = fobj.read(1024)
        if not fb:
            break
        md5.update(fb)
    fobj.close()
    return md5.hexdigest()

def main(fout):

    zfile = zipfile.ZipFile(apkname)
    AndroidManifestXml = zfile.read("AndroidManifest.xml")
    zfile.close()
    
    ftemp = file("temp.xml", "wb")
    ftemp.write(AndroidManifestXml)
    ftemp.close()
    os.system("java -jar AXMLPrinter2.jar temp.xml > temp.txt")
    page = file("temp.txt", "rb").read().strip()
    os.system("del temp.xml ")
    os.system("del temp.txt ")
    assert page.endswith("</manifest>"), u"manifest read error..."

    pkg_ver_code = int(re.findall("android:versionCode=\"([0-9]+)\"", page)[0])
    pkg_ver_name = re.findall("android:versionName=\"(.*?)\"", page)[0].strip() # string
    pkg_ver_name = "".join(pkg_ver_name.split())

    readme = file("readme.txt").read().strip().replace("\n\n\n", "\n\n")
    assert readme.find("=")==-1, "xp.."
    readme = "\n\n".join(readme.split("\n\n")[:8]).strip()
    readme = readme.replace("\n","<br/>")

    readme2 = file("readme.txt").read().strip().replace("\n\n\n", "\n\n")
    lx = readme2.split("\n\n")[:32]

    def writeln(str):
        fout.write(str)
        fout.write("\r\n")
    writeln("[config.apk.1]")
    writeln("apk.md5=%s" % mkmd5fromfile(apkname))
    writeln("ver.code=%d" % pkg_ver_code)
    writeln("ver.name=%s" % pkg_ver_name)
    writeln("apk.name=%s" % apkname)
    writeln("apk.size=%d" % os.path.getsize(apkname))
    writeln("apk.uplog=%s" % readme)
    writeln("chance=%d" % int(10000 * chance))
    writeln("")

    writeln("[TaobaoApi]")
    writeln("TaobaoApi.magic=5")
    writeln("TaobaoApi.serverUrl=http://gw.api.taobao.com/router/rest")
    writeln("TaobaoApi.appKey=12581578")
    writeln("TaobaoApi.appSecret=6b7c4877be6aac021bb4274a643ec9db")
    writeln("TaobaoApi.setNick=孤天白鹤")
    writeln("TaobaoApi.setKeyword=礼物")
    writeln("")
    writeln("")
    

    writeln("[config.apk.2]")
    for ix in lx:
        ks = ix.strip().split("\n")
        writeln("apk.%s=%s -- %s" % (ks[0].split("_")[2],
            "<br/>".join([" ".join(k[k.find(".")+1:].strip().split()) for k in ks[1:]]),
                                  ks[0].split("_")[4]))
    writeln("")

    foo = file("log.txt", "wb")
    count = 0
    for ix in lx[::-1]:
        ks = ix.strip().split("\n")
        count = count + 1
        foo.write("%d. %s -- %s" % (count,
            " ".join([" ".join(k[k.find(".")+1:].strip().split()) for k in ks[1:]]),
                                   ks[0].split("_")[4]))
        foo.write("\r\n")
    foo.close()
        

if __name__ == "__main__":
    fout = file("upgrade.ini", "wb")
    main(fout)
    fout.close()

    fout = file("upgrade26.ini", "wb")
    main(fout)
    fout.close()
    
    fout = file("upgrade51.ini", "wb")
    main(fout)
    fout.close()    
    print "ok"
    os.system("@Pause")
